"""装饰器工具模块"""
from functools import wraps
from flask import session, redirect, url_for, request, jsonify

def login_required(f):
    """登录验证装饰器"""
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'username' not in session:
            # 检查是否是API请求
            if request.path.startswith('/api/') or request.headers.get('X-Requested-With') == 'XMLHttpRequest':
                return jsonify({'success': False, 'message': '未登录或会话已过期'}), 401
            return redirect(url_for('auth.login_page'))
        return f(*args, **kwargs)
    return wrap

